// const baseUrl = 'http://douban.newfq.com/'
import cfg from '../../common/config/index.js'

Page({
  data: {
    movie: {}
  },
  onLoad: function (options) {
    const { id } = options;
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: `${cfg.domain}/detail/${id}`,
      success: (res) => {
        const movie = res.data.data;
        this.setData({
          movie
        });

        //wx.setStorageSync('historyData',[{a:1,b:1},{a:2,b:2},{a:3,b:3}])
        // wx.removeStorageSync('historyData')
        let historyData = wx.getStorageSync('historyData');

        if (historyData){
          historyData = JSON.parse(historyData);
        }else{
          historyData = [];
        }

        //判断当前这个和本地的这个是否一样
        if (historyData[0] && historyData[0].id && historyData[0].id != movie._id){
          historyData.unshift({
            id: movie._id,
            poster: movie.poster,
            title: movie.title,
            rate: movie.rate,
            time: movie.pubdate,
            country: movie.country,
          });
        }

        if (historyData.length>2){
          historyData.length = 2;
        }
        wx.setStorageSync('historyData', JSON.stringify(historyData));

        wx.hideLoading();
      }
    })
  }

})